home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / win / tkWinClipboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  6.7 KB  |  292 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkWinClipboard.c --
  3.  *
  4.  *    This file contains functions for managing the clipboard.
  5.  *
  6.  * Copyright (c) 1995 Sun Microsystems, Inc.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * SCCS: @(#) tkWinClipboard.c 1.8 97/05/20 17:01:13
  12.  */
  13.  
  14. #include "tkWinInt.h"
  15. #include "tkSelect.h"
  16.  
  17.  
  18. /*
  19.  *----------------------------------------------------------------------
  20.  *
  21.  * TkSelGetSelection --
  22.  *
  23.  *    Retrieve the specified selection from another process.  For
  24.  *    now, only fetching XA_STRING from CLIPBOARD is supported.
  25.  *    Eventually other types should be allowed.
  26.  * 
  27.  * Results:
  28.  *    The return value is a standard Tcl return value.
  29.  *    If an error occurs (such as no selection exists)
  30.  *    then an error message is left in interp->result.
  31.  *
  32.  * Side effects:
  33.  *    None.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. int
  39. TkSelGetSelection(interp, tkwin, selection, target, proc, clientData)
  40.     Tcl_Interp *interp;        /* Interpreter to use for reporting
  41.                  * errors. */
  42.     Tk_Window tkwin;        /* Window on whose behalf to retrieve
  43.                  * the selection (determines display
  44.                  * from which to retrieve). */
  45.     Atom selection;        /* Selection to retrieve. */
  46.     Atom target;        /* Desired form in which selection
  47.                  * is to be returned. */
  48.     Tk_GetSelProc *proc;    /* Procedure to call to process the
  49.                  * selection, once it has been retrieved. */
  50.     ClientData clientData;    /* Arbitrary value to pass to proc. */
  51. {
  52.     char *data, *buffer, *destPtr;
  53.     HGLOBAL handle;
  54.     int result, length;
  55.  
  56.     if ((selection == Tk_InternAtom(tkwin, "CLIPBOARD"))
  57.         && (target == XA_STRING)) {
  58.     if (OpenClipboard(NULL)) {
  59.         handle = GetClipboardData(CF_TEXT);
  60.         if (handle != NULL) {
  61.         data = GlobalLock(handle);
  62.         length = strlen(data);
  63.         buffer = ckalloc(length+1);
  64.         destPtr = buffer;
  65.         while (*data != '\0') {
  66.             if (*data != '\r') {
  67.             *destPtr = *data;
  68.             destPtr++;
  69.             }
  70.             data++;
  71.         }
  72.         *destPtr = '\0';
  73.         GlobalUnlock(handle);
  74.         CloseClipboard();
  75.         result = (*proc)(clientData, interp, buffer);
  76.         ckfree(buffer);
  77.         return result;
  78.         }
  79.         CloseClipboard();
  80.     }
  81.     }
  82.  
  83.     Tcl_AppendResult(interp, Tk_GetAtomName(tkwin, selection),
  84.     " selection doesn't exist or form \"", Tk_GetAtomName(tkwin, target),
  85.     "\" not defined", (char *) NULL);
  86.     return TCL_ERROR;
  87. }
  88.  
  89. /*
  90.  *----------------------------------------------------------------------
  91.  *
  92.  * TkSetSelectionOwner --
  93.  *
  94.  *    This function claims ownership of the specified selection.
  95.  *    If the selection is CLIPBOARD, then we empty the system
  96.  *    clipboard.
  97.  *
  98.  * Results:
  99.  *    None.
  100.  *
  101.  * Side effects:
  102.  *    Empties the system clipboard, and claims ownership.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. void
  108. XSetSelectionOwner(display, selection, owner, time)
  109.     Display* display;
  110.     Atom selection;
  111.     Window owner;
  112.     Time time;
  113. {
  114.     HWND hwnd = owner ? TkWinGetHWND(owner) : NULL;
  115.     Tk_Window tkwin;
  116.  
  117.     /*
  118.      * This is a gross hack because the Tk_InternAtom interface is broken.
  119.      * It expects a Tk_Window, even though it only needs a Tk_Display.
  120.      */
  121.  
  122.     tkwin = (Tk_Window)tkMainWindowList->winPtr;
  123.  
  124.     if (selection == Tk_InternAtom(tkwin, "CLIPBOARD")) {
  125.  
  126.     /*
  127.      * Only claim and empty the clipboard if we aren't already the
  128.      * owner of the clipboard.
  129.      */
  130.  
  131.     if (GetClipboardOwner() != hwnd) {
  132.         OpenClipboard(hwnd);
  133.         EmptyClipboard();
  134.         SetClipboardData(CF_TEXT, NULL);
  135.         CloseClipboard();
  136.     }
  137.     }
  138. }
  139.  
  140. /*
  141.  *----------------------------------------------------------------------
  142.  *
  143.  * TkWinClipboardRender --
  144.  *
  145.  *    This function supplies the contents of the clipboard in
  146.  *    response to a WM_RENDERFORMAT message.
  147.  *
  148.  * Results:
  149.  *    None.
  150.  *
  151.  * Side effects:
  152.  *    Sets the contents of the clipboard.
  153.  *
  154.  *----------------------------------------------------------------------
  155.  */
  156.  
  157. void
  158. TkWinClipboardRender(dispPtr, format)
  159.     TkDisplay *dispPtr;
  160.     UINT format;
  161. {
  162.     TkClipboardTarget *targetPtr;
  163.     TkClipboardBuffer *cbPtr;
  164.     HGLOBAL handle;
  165.     char *buffer, *p, *endPtr;
  166.     int length;
  167.  
  168.     for (targetPtr = dispPtr->clipTargetPtr; targetPtr != NULL;
  169.         targetPtr = targetPtr->nextPtr) {
  170.     if (targetPtr->type == XA_STRING)
  171.         break;
  172.     }
  173.     length = 0;
  174.     if (targetPtr != NULL) {
  175.     for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  176.         cbPtr = cbPtr->nextPtr) {
  177.         length += cbPtr->length;
  178.         for (p = cbPtr->buffer, endPtr = p + cbPtr->length;
  179.             p < endPtr; p++) {
  180.         if (*p == '\n') {
  181.             length++;
  182.         }
  183.         }
  184.     }
  185.     }
  186.     handle = GlobalAlloc(GMEM_MOVEABLE|GMEM_DDESHARE, length+1);
  187.     if (!handle) {
  188.     return;
  189.     }
  190.     buffer = GlobalLock(handle);
  191.     if (targetPtr != NULL) {
  192.     for (cbPtr = targetPtr->firstBufferPtr; cbPtr != NULL;
  193.         cbPtr = cbPtr->nextPtr) {
  194.         for (p = cbPtr->buffer, endPtr = p + cbPtr->length;
  195.             p < endPtr; p++) {
  196.         if (*p == '\n') {
  197.             *buffer++ = '\r';
  198.         }
  199.         *buffer++ = *p;
  200.         }
  201.     }
  202.     }
  203.     *buffer = '\0';
  204.     GlobalUnlock(handle);
  205.     SetClipboardData(CF_TEXT, handle);
  206.     return;
  207. }
  208.  
  209. /*
  210.  *----------------------------------------------------------------------
  211.  *
  212.  * TkSelUpdateClipboard --
  213.  *
  214.  *    This function is called to force the clipboard to be updated
  215.  *    after new data is added.
  216.  *
  217.  * Results:
  218.  *    None.
  219.  *
  220.  * Side effects:
  221.  *    Clears the current contents of the clipboard.
  222.  *
  223.  *----------------------------------------------------------------------
  224.  */
  225.  
  226. void
  227. TkSelUpdateClipboard(winPtr, targetPtr)
  228.     TkWindow *winPtr;
  229.     TkClipboardTarget *targetPtr;
  230. {
  231.     HWND hwnd = TkWinGetHWND(winPtr->window);
  232.  
  233.     OpenClipboard(hwnd);
  234.     EmptyClipboard();
  235.     SetClipboardData(CF_TEXT, NULL);
  236.     CloseClipboard();
  237. }
  238.  
  239. /*
  240.  *--------------------------------------------------------------
  241.  *
  242.  * TkSelEventProc --
  243.  *
  244.  *    This procedure is invoked whenever a selection-related
  245.  *    event occurs. 
  246.  *
  247.  * Results:
  248.  *    None.
  249.  *
  250.  * Side effects:
  251.  *    Lots:  depends on the type of event.
  252.  *
  253.  *--------------------------------------------------------------
  254.  */
  255.  
  256. void
  257. TkSelEventProc(tkwin, eventPtr)
  258.     Tk_Window tkwin;        /* Window for which event was
  259.                  * targeted. */
  260.     register XEvent *eventPtr;    /* X event:  either SelectionClear,
  261.                  * SelectionRequest, or
  262.                  * SelectionNotify. */
  263. {
  264.     if (eventPtr->type == SelectionClear) {
  265.     TkSelClearSelection(tkwin, eventPtr);
  266.     }
  267. }
  268.  
  269. /*
  270.  *----------------------------------------------------------------------
  271.  *
  272.  * TkSelPropProc --
  273.  *
  274.  *    This procedure is invoked when property-change events
  275.  *    occur on windows not known to the toolkit.  This is a stub
  276.  *    function under Windows.
  277.  *
  278.  * Results:
  279.  *    None.
  280.  *
  281.  * Side effects:
  282.  *    None.
  283.  *
  284.  *----------------------------------------------------------------------
  285.  */
  286.  
  287. void
  288. TkSelPropProc(eventPtr)
  289.     register XEvent *eventPtr;        /* X PropertyChange event. */
  290. {
  291. }
  292.